
let layer = layui.layer
let form = layui.form
//重置密码
// ***********************重置密码的form表单
let str = `<form id="repwd-form" class="layui-form" style="margin: 15px 30px 0 0">
      <!-- 第一行：密码 -->
      <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
          <input type="password" name="password" required lay-verify="required" placeholder="请输入新密码" autocomplete="off" class="layui-input oldPwd">
        </div>
      </div>
      <!-- 第二行：确认密码 -->
      <div class="layui-form-item">
        <label class="layui-form-label">确认密码</label>
        <div class="layui-input-block">
          <input type="password" required lay-verify="required|pwd" placeholder="请重复输入新密码" autocomplete="off" class="layui-input">
        </div>
      </div>
      <!-- 第三行：按钮 -->
      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
    </form>`;

$('tbody').on('click', '.reset', function () {
  let id = $(this).attr('_pwd')
  layer.open({
    type: 1,
    title: '重置密码',
    area: ['500px', '250px'],
    content: str,
    success: function (dom, index) {
      pwd(); //验证密码
      //提交修改的密码
      addPwd(id, index);
    }
  })
})

//验证表单
function pwd() {
  form.verify({
    pwd: function (val, dom) {
      if (val != $('.oldPwd').val()) {
        return '必须新密码相同'
      }
    }
  })
}
//提交密码功能
function addPwd(_id, i) {
  $('.layui-form').submit(function (e) {
    e.preventDefault();
    $.ajax({
      url: 'admin/users/' + _id,
      type: 'put',
      data: $(this).serialize(),
      success: function (res) {
        layer.msg(res.message)
        if (res.status == 0) {
          layer.close(i)
        }
      }
    })
  })
}




